import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../ColorHelper.dart';

class DialogUtil {
  static Future<dynamic> dialog(Widget widget,
      {bool canDismiss = true, bool canPop = true, Color? barrierColor}) {
    return Get.dialog(
        PopScope(
          canPop: canPop,
          child: Material(
            type: MaterialType.transparency,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [widget],
              ),
            ),
          ),
        ),
        barrierDismissible: canDismiss, barrierColor: barrierColor);
  }

  static Future<dynamic> dialogPositioned(Widget widget,
      {
        bool canDismiss = true,
        bool canPop = true,
        Color? barrierColor,
        double? top,
        double? left,
        double? bottom,
        double? right
      }) {
    return Get.dialog(
        PopScope(
          canPop: canPop,
          child: Material(
            type: MaterialType.transparency,
            child: Stack(
              children: [
                Positioned(
                  top: top,
                    left: left,
                    bottom: bottom,
                    right: right,
                    child: widget
                )
              ],
            ),
          ),
        ),
        barrierDismissible: canDismiss, barrierColor: barrierColor);
  }

  static Future<dynamic> bottomSheet(Widget widget) {
    return Get.bottomSheet(
        Material(
          type: MaterialType.transparency,
          child: widget,
        ),
        elevation: 0);
  }



  static Future<void> defaultDialog(String title, String content,
      {confirmCallback, cancelCallback}) {
    return Get.defaultDialog(
        title: title,
        titlePadding: EdgeInsets.zero,
        middleText: content,
        confirm: TextButton(
            onPressed: () {
              Get.back();
              if (confirmCallback != null) {
                confirmCallback();
              }

            },
            child: Text(
              'confirm'.tr,
              style: const TextStyle(color: ColorHelper.blue),
            )),
        cancel: TextButton(
            onPressed: () {
              Get.back();
              if (cancelCallback != null) {
                cancelCallback();
              }

            },
            child: Text(
              'cancel'.tr,
              style: const TextStyle(color: ColorHelper.gray9B),
            )));
  }

  static Widget buildChoices(List<String> choices, callback) => SafeArea(
    child: Container(
      height: (choices.length + 1) * 35.h + 5.h + (choices.length - 1).h,
      color: ColorHelper.grayF6,
      child: Column(
        children: [
          ListView.separated(
            itemBuilder: (ctx, index) {
              return GestureDetector(
                onTap: () async {
                  if (callback != null) {
                    await callback(choices[index]);
                  }
                  Get.back();
                },
                child: Container(
                  color: ColorHelper.white,
                  height: 35.h,
                  alignment: Alignment.center,
                  child: Text(choices[index].tr),
                ),
              );
            },
            separatorBuilder: (ctx, index) {
              return Container(
                height: 1.h,
              );
            },
            itemCount: choices.length,
            shrinkWrap: true,
            padding: EdgeInsets.zero,
          ),
          GestureDetector(
            onTap: () {
              Get.back();
            },
            child: Container(
              height: 35.h,
              color: ColorHelper.white,
              alignment: Alignment.center,
              margin: EdgeInsets.only(top: 5.h),
              child: Text(
                'cancel'.tr,
                style: const TextStyle(color: ColorHelper.red),
              ),
            ),
          )
        ],
      ),
    ),
  );


}
